//
//  MainViewController.m
//  网易公开课项目
//
//  Created by 林 艺 on 13-11-10.
//  Copyright (c) 2013年 Marklin. All rights reserved.
//

#import "MainViewController.h"
#import "BaseNavigationController.h"
#import "RecommondViewController.h"
#import "CourseViewController.h"
#import "BookmarkViewController.h"
#import "DownloadViewController.h"
#import "MoreViewController.h"
#import "AppDelegate.h"
#import "LoadDataViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBar.hidden = YES;
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self _initTabbarView];
    [self _initViewControllers];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)_initViewControllers
{
    RecommondViewController *recommond = [[RecommondViewController alloc] initWithNibName:@"RecommondViewController" bundle:nil];
    CourseViewController *course = [[CourseViewController alloc] initWithNibName:@"CourseViewController" bundle:nil];
    BookmarkViewController *bookmark = [[BookmarkViewController alloc] initWithNibName:@"BookmarkViewController" bundle:nil];
    DownloadViewController *download = [[DownloadViewController alloc] initWithNibName:@"DownloadViewController" bundle:nil];
    MoreViewController *more = [[MoreViewController alloc] initWithNibName:@"MoreViewController" bundle:nil];
    
    NSArray *views = @[recommond, course, bookmark, download, more];
    NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithCapacity:5];

    for (UIViewController *viewController in views)
    {
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:viewController];
        [viewControllers addObject:nav];
    }
    
    self.viewControllers = viewControllers;
    self.selectedIndex = 0;
}

- (void)_initTabbarView
{
    //创建tabbarView
     _tabbarView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-57, ScreenWidth, 57)];
    [self.view addSubview:_tabbarView];
    
    //添加背景图片
    UIImageView *tabbarBackgroundView = [[UIImageView alloc] initWithFrame:self.tabbarView.bounds];
    tabbarBackgroundView.image = [UIImage imageNamed:@"bottombar.png"];
    tabbarBackgroundView.backgroundColor = [UIColor clearColor];
    [self.tabbarView addSubview:tabbarBackgroundView];
    
    //添加按钮
    _buttonNormalImages = @[@"btn_recommend.png", @"btn_courses.png", @"btn_bookmark.png", @"btn_download.png", @"btn_more.png"];
    
    _buttonHighlitedImages = @[@"btn_recommend_active.png", @"btn_courses_active.png", @"btn_bookmark_active.png", @"btn_download_active.png", @"btn_more_active.png"];
    NSArray *labelArray = @[@"推荐", @"课程", @"收藏夹", @"下载管理", @"更多"];
    
    for (int i = 0; i < _buttonNormalImages.count; i++)
    {
        NSString *backImageName = _buttonNormalImages[i];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(ScreenWidth/_buttonNormalImages.count*i, 8, ScreenWidth/_buttonNormalImages.count, 49);
        button.tag = i+TabbarButtonTag;
        if (i == 0)
        {
            [button setImage:[UIImage imageNamed:_buttonHighlitedImages[0]] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:_buttonHighlitedImages[0]] forState:UIControlStateHighlighted];
            _preButton = button;
        }
        else
        {
            [button setImage:[UIImage imageNamed:backImageName] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:backImageName] forState:UIControlStateHighlighted];
        }
        [button addTarget:self action:@selector(selectTab:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabbarView addSubview:button];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/_buttonNormalImages.count*i, 33, ScreenWidth/_buttonNormalImages.count, 20)];
        label.font = [UIFont systemFontOfSize:10];
        label.tag = TabbarLabelTag+i;
        label.text = [labelArray objectAtIndex:i];
        if (i == 0)
        {
            label.textColor = [UIColor brownColor];
            _preLabel = label;
        }
        else
        {
            label.textColor = [UIColor whiteColor];
        }
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        [self.tabbarView addSubview:label];
    }
    
    
    
    
}

- (void)selectTab:(UIButton *)button
{
    if (_preButton != nil)
    {
        [_preButton setImage:[UIImage imageNamed:[_buttonNormalImages objectAtIndex:_preButton.tag-TabbarButtonTag]] forState:UIControlStateNormal];
        [_preButton setImage:[UIImage imageNamed:[_buttonNormalImages objectAtIndex:_preButton.tag-TabbarButtonTag]] forState:UIControlStateHighlighted];
        _preLabel.textColor = [UIColor whiteColor];
    }
    
    self.selectedIndex = button.tag-TabbarButtonTag;
    [button setImage:[UIImage imageNamed:[_buttonHighlitedImages objectAtIndex:button.tag-TabbarButtonTag]] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:[_buttonHighlitedImages objectAtIndex:button.tag-TabbarButtonTag]] forState:UIControlStateHighlighted];
    UILabel *label = (UILabel *)[self.view viewWithTag:button.tag-TabbarButtonTag+TabbarLabelTag];
    label.textColor = [UIColor brownColor];
    _preLabel = label;
    _preButton = button;
    
}

@end
